package com.kitty.game.chengzhangmiji.service;

import com.alibaba.fastjson.JSONObject;
import com.kitty.common.cache.RedisService;
import com.kitty.game.activity.model.product.ActivityType;
import com.kitty.game.activity.service.ActivityService;
import com.kitty.game.chengzhangmiji.message.*;
import com.kitty.game.drop.newservice.NewDropService;
import com.kitty.game.equip.message.RespNotifyMiscEx;
import com.kitty.game.player.model.PlayerCzmjInfo;
import com.kitty.game.role.model.Role;
import com.kitty.game.role.service.RoleService;
import com.kitty.common.model.Reason;
import com.kitty.mina.cache.DataCache;
import com.kitty.mina.message.MessagePusher;
import com.kitty.common.utils.DecodeSendMessage;
import org.nutz.lang.util.NutMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class ChengZhangMiJiService {
    @Autowired
    RoleService roleService;
    @Autowired
    ActivityService activityService;
    @Autowired
    NewDropService newDropService;
    @Autowired
    RedisService redisService;
    public  List<CZMJItemInfo> setCzmjListInfo(String type,List<String> czmjRecords,int level){
        List<CZMJItemInfo> list = new ArrayList<>();
        String reward = "";
        if("0".equals(type)){
            reward = DataCache.REDIS_DATA.get("czmj_putong_reward");
        }else if("1".equals(type)){
            reward = DataCache.REDIS_DATA.get("czmj_gaoji_reward");
        }else if("2".equals(type)){
            reward = DataCache.REDIS_DATA.get("czmj_juepin_reward");
        }else{
            reward = DataCache.REDIS_DATA.get("czmj_task");
        }
        JSONObject putongArr = JSONObject.parseObject(reward);
        for (Map.Entry<String, Object> stringObjectEntry : putongArr.entrySet()) {
            int pos = Integer.parseInt(stringObjectEntry.getKey());
            CZMJItemInfo czmjItemInfo  = new CZMJItemInfo();
            NutMap nutMap = DecodeSendMessage.buildSendRewardMessage(stringObjectEntry.getValue().toString());
            czmjItemInfo.setName(nutMap.getString("data"));
            czmjItemInfo.setPos((byte)pos);
            czmjItemInfo.setUse((byte)0);
            if(czmjRecords.contains(type+pos)){
                czmjItemInfo.setUse((byte)2);
            }else{
                if(pos<=level){
                    czmjItemInfo.setUse((byte)1);
                }else{
                    czmjItemInfo.setUse((byte)0);
                }
            }
            list.add(czmjItemInfo);
        }
        return list;
    }


    public void openChengZhangMiJi(Role role){
        RespOpenChengZhangMiJi respOpenChengZhangMiJi = new RespOpenChengZhangMiJi();
        PlayerCzmjInfo playerCzmjInfo =  role.getExtendBox().getPlayerCzmjInfo();
        if(playerCzmjInfo.getStartTime() ==0){
            Calendar cal = Calendar.getInstance();
            cal.setTime(new Date());
            cal.set(Calendar.HOUR_OF_DAY, 5);
            cal.set(Calendar.MINUTE, 0);
            cal.add(Calendar.DAY_OF_MONTH, -1); //解决周日会出现 并到下一周的情况
            cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
            playerCzmjInfo.setStartTime((int)(cal.getTimeInMillis()/1000));
            playerCzmjInfo.setEndTime((int)(cal.getTimeInMillis()/1000+3600*24*7));
        }
        respOpenChengZhangMiJi.setStartTime(playerCzmjInfo.getStartTime());
        respOpenChengZhangMiJi.setEndTime(playerCzmjInfo.getEndTime());
        respOpenChengZhangMiJi.setEndTime2(playerCzmjInfo.getEndTime());
        //领取奖励信息  -- 和 购买类型信息  每周刷新
        List<String> czmjRewards = playerCzmjInfo.getCzmjRewards();
        if(!czmjRewards.contains("普通")){
            czmjRewards.add("普通");
        }
        //任务领取信息  --每日刷新
        List<String> czmjTasks = playerCzmjInfo.getCzmjTask();

        respOpenChengZhangMiJi.setSumExp(playerCzmjInfo.getSumExp());
        respOpenChengZhangMiJi.setLev((byte)playerCzmjInfo.getCzmjLevel());
        respOpenChengZhangMiJi.setExp(playerCzmjInfo.getExp());
        int level = playerCzmjInfo.getCzmjLevel();
        if(czmjRewards.contains("高级")){
            respOpenChengZhangMiJi.setCzmjGaoji((byte)1);
        }
        if(czmjRewards.contains("绝品")){
            respOpenChengZhangMiJi.setCzmjJuepin((byte)2);
        }
        // 普通奖励
        List<CZMJItemInfo> putongList = setCzmjListInfo("0", czmjRewards, level);
        respOpenChengZhangMiJi.setCzmj1ItemInfoList(putongList);
        //高级奖励
        List<CZMJItemInfo> gaojiList = setCzmjListInfo("1", czmjRewards, level);
        respOpenChengZhangMiJi.setCzmj2ItemInfoList(gaojiList);
        //绝品奖励
        List<CZMJItemInfo> juepinList = setCzmjListInfo("2", czmjRewards, level);
        respOpenChengZhangMiJi.setCzmj3ItemInfoList(juepinList);

        // 任务信息
        String czmj_task = DataCache.REDIS_DATA.get("czmj_task");
        JSONObject parse = JSONObject.parseObject(czmj_task);
        List<CZMJTaskInfo> czmjTaskInfoList = new ArrayList<>();
        for (Map.Entry<String, Object> stringObjectEntry : parse.entrySet()) {
            int num = Integer.parseInt(stringObjectEntry.getKey());
            JSONObject taskObj = JSONObject.parseObject(stringObjectEntry.getValue().toString());
            CZMJTaskInfo czmjTaskInfo = new CZMJTaskInfo();
            String taskName = taskObj.getString("taskName");
            short zongCiShu = taskObj.getShort("number");
            czmjTaskInfo.setTaskId((short)num);
            czmjTaskInfo.setTaskName(taskName);
            czmjTaskInfo.setTaskRemark(taskObj.getString("taskRemark"));
            czmjTaskInfo.setZongCishu(zongCiShu);
            czmjTaskInfo.setTaskExp(taskObj.getShort("exp"));
            int doStep = 0;
            if("师门任务".equals(taskName)){
                doStep = activityService.getFinishCount(role, ActivityType.SCHOOL_TASK) ;
            }else if("除暴任务".equals(taskName)){
                doStep = activityService.getFinishCount(role, ActivityType.CHU_BAO_TASK) ;
            }else if("助人为乐".equals(taskName)){
                doStep = activityService.getFinishCount(role, ActivityType.HELP_PEOPLE_TASK);
            }else if("通天塔".equals(taskName)){
                doStep = activityService.getFinishCount(role, ActivityType.TOWER_TASK);
            }else if("副本".equals(taskName)){
                doStep = activityService.getFinishCount(role, ActivityType.DUGEON_TASK);
            }else if("英雄会".equals(taskName)){
                doStep = activityService.getFinishCount(role, ActivityType.YING_XIONG_HUI);
            }
            czmjTaskInfo.setState((byte)1);
            //判断是否完成，
            if(doStep >= zongCiShu){
                doStep = zongCiShu;
                //判断是否已经领取。
                if(czmjTasks.contains(taskName)){
                    czmjTaskInfo.setState((byte)2);
                }
            }else{
                czmjTaskInfo.setState((byte)0);
            }
            czmjTaskInfo.setCishu((short)doStep);
            czmjTaskInfoList.add(czmjTaskInfo);
        }
        respOpenChengZhangMiJi.setCzmjITaskInfoList(czmjTaskInfoList);
        MessagePusher.pushMessage(role, respOpenChengZhangMiJi);
    }
    public void fetchLevelBonus(Role role, ReqCzmjLevelBonus reqCzmjLevelBonus){
        List<String> czmjRewards = role.getExtendBox().getPlayerCzmjInfo().getCzmjRewards();
        String type = "普通";
        if(reqCzmjLevelBonus.getType() == 1){
            type = "高级";
        }
        if(reqCzmjLevelBonus.getType() == 2){
            type = "绝品";
        }
        if(!czmjRewards.contains(type)){
            MessagePusher.pushMessage(role, new RespNotifyMiscEx("非法请求！"));
            return;
        }
        String rewardName = reqCzmjLevelBonus.getType()+""+reqCzmjLevelBonus.getLevel()+"";

        int level = role.getExtendBox().getPlayerCzmjInfo().getCzmjLevel();
        if(czmjRewards.contains(rewardName) || level<reqCzmjLevelBonus.getLevel()){
            MessagePusher.pushMessage(role, new RespNotifyMiscEx("非法请求！"));
            return;
        }
        czmjRewards.add(rewardName);
        //这里要发送奖励。

        String reward = "";
        if(0 == reqCzmjLevelBonus.getType()){
            reward = DataCache.REDIS_DATA.get("czmj_putong_reward");
        }else if(1==reqCzmjLevelBonus.getType()){
            reward = DataCache.REDIS_DATA.get("czmj_gaoji_reward");
        }else if(2 == reqCzmjLevelBonus.getType()){
            reward = DataCache.REDIS_DATA.get("czmj_juepin_reward");
        }
        JSONObject putongArr = JSONObject.parseObject(reward);
        String desc = putongArr.get(reqCzmjLevelBonus.getLevel() + "").toString();
        newDropService.addRewardTask( role,desc,"");
        MessagePusher.pushMessage(role, new RespNotifyMiscEx("领取成功"));
        openChengZhangMiJi(role);
    }
    public void byCzmjActive(Role role, ReqCzmjActiveType reqCzmjActiveType){
        int count = 0;
        String type = "";
        if(reqCzmjActiveType.getType() == 1){
            count = 3800;
            type = "高级";
        }else {
            count = 9800;
            type = "绝品";
        }
        roleService.subGoldOrCharge(role, count, Reason.BUY_ITEM);
        List<String> czmjRewards = role.getExtendBox().getPlayerCzmjInfo().getCzmjRewards();
        czmjRewards.add(type);
        openChengZhangMiJi(role);
    }
    //领取经验
    public void fetchTaskBonus(Role role, ReqCzmjTaskBonus reqCzmjTaskBonus){
        short taskId = reqCzmjTaskBonus.getTaskId();
        PlayerCzmjInfo playerCzmjInfo = role.getExtendBox().getPlayerCzmjInfo();
        // 任务信息
        String czmj_task = DataCache.REDIS_DATA.get("czmj_task");
        JSONObject parse = JSONObject.parseObject(czmj_task);
        JSONObject jsonObject = parse.getJSONObject(taskId + "");
        String taskName = jsonObject.getString("taskName");
        List<String> czmjTasks = playerCzmjInfo.getCzmjTask();
        //判断 是否已经领取过了。
        if(czmjTasks.contains(taskName)){
            MessagePusher.pushMessage(role, new RespNotifyMiscEx("非法请求！"));
            return;
        }
        Integer addExp = jsonObject.getInteger("exp");
        int sumExp = playerCzmjInfo.getSumExp();
        int exp =  playerCzmjInfo.getExp();
        if(exp + addExp >= sumExp){
            int level = (exp + addExp)/sumExp;
            if(playerCzmjInfo.getCzmjLevel()<30){
                playerCzmjInfo.setCzmjLevel(playerCzmjInfo.getCzmjLevel()+level);
            }
            playerCzmjInfo.setExp((exp + addExp)%sumExp);
        }else{
            playerCzmjInfo.setExp(exp + addExp);
        }
        //领取成功
        czmjTasks.add(taskName);
        openChengZhangMiJi(role);
    }

    public static void main(String[] args) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(new Date());
        cal.set(Calendar.HOUR_OF_DAY, 5);
        cal.set(Calendar.MINUTE, 0);
        cal.add(Calendar.DAY_OF_MONTH, -1); //解决周日会出现 并到下一周的情况
        cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
        System.out.println( cal.getTimeInMillis()/1000);


    }
}
